package com.tms.common.entity;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/*-----------------------------------
 *   @Time: 2022/9/5 15:13
 *   @Description: AJAX 响应封装类
-----------------------------------*/
@Data
public class ResultVO {
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    private ResultVO() {
    }

    // 响应成功
    public static ResultVO ok() {
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(true);
        resultVO.setCode(ResultCode.SUCCESS);
        resultVO.setMessage("成功");
        return resultVO;
    }

    // 响应失败
    public static ResultVO error() {
        ResultVO resultVO = new ResultVO();
        resultVO.setSuccess(false);
        resultVO.setCode(ResultCode.ERROR);
        resultVO.setMessage("失败");
        return resultVO;
    }

    public ResultVO data(String k, Object v) {
        this.data.put(k, v);
        return this;
    }

    public ResultVO message(String message) {
        this.setMessage(message);
        return this;
    }

    public ResultVO code(Integer code) {
        this.code = code;
        return this;
    }
}
